// import {
//   getAdContentTable,
//   editAdContentRecord,
//   deleteAdContentRecord
// } from '../../api/content'
import {
  AD_CONTENT_LIST,
  AD_CONTENT_CURRENT_ROW,
  AD_CONTENT_DATA_COUNT,
  AD_CONTENT_HISTORY_DATA,
  AD_CONTENT_PAGE_SIZE
} from '../motations-type'
import qqgroup from '@/assets/images/qq-group2.jpg'

export default {
  state: {
    adContentList: [],
    adContentDataCount: null,
    adContentPageSize: 20,
    adContentHistoryData: [],
    row: {
      img: '',
      ctime: '',
      title: '',
      state: '',
      openState: false
    }
  },
  getters: {
    adContentList: state => state.adContentList
  },
  mutations: {
    [AD_CONTENT_LIST] (state, data) {
      state.adContentList = data
    },
    [AD_CONTENT_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [AD_CONTENT_DATA_COUNT] (state, data) {
      state.adContentDataCount = data
    },
    [AD_CONTENT_HISTORY_DATA] (state, data) {
      state.adContentHistoryData = data
    },
    [AD_CONTENT_PAGE_SIZE] (state, data) {
      state.adContentPageSize = data
    }
  },
  actions: {
    async getAdContentList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      newData = [...newData, {
        id: 1,
        img: qqgroup,
        title: '汽车',
        ctime: '2019-3-27 15:00:00',
        state: 0,
        openState: true
      }]
      commit(AD_CONTENT_LIST, newData)
      commit(AD_CONTENT_DATA_COUNT, newData.length)
      if (newData.length < rootState.adContent.adContentPageSize) {
        commit(AD_CONTENT_HISTORY_DATA, newData)
      } else {
        commit(AD_CONTENT_HISTORY_DATA, newData.slice(0, rootState.adContent.adContentPageSize))
      }
    },
    async editAdContentData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      await editAdContentRecord(data)
    },
    async deleteAdContentData ({ commit, rootState }, playload) {
      await deleteAdContentRecord(playload)
    },
    async getAdContentCurrentRow ({ commit, rootState }, row) {
      commit(AD_CONTENT_CURRENT_ROW, row)
    },
    async changeAdContentPage ({ commit, rootState }, index) {
      let data = rootState.adContent.adContentList
      let _start = (index - 1) * rootState.adContent.adContentPageSize
      let _end = index * rootState.adContent.adContentPageSize
      commit(BANNER_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeAdContentPageSize ({ commit, rootState }, value) {
      commit(AD_CONTENT_PAGE_SIZE, value)
    }
  }
}
